﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace YY.Admin.ViewModels.Control
{
    public class PaginationDataGridViewModel<T> : BindableBase
    {
        private int _pageIndex = 1;
        private int _dataCountPerPage = 10;
        private int _totalCount;
        private int _maxPageCount;
        private ObservableCollection<T> _data;
        //private string _pageInfo;

        public int MyProperty { get; private set; }

        public int PageIndex
        {
            get => _pageIndex;
            set => SetProperty(ref _pageIndex, value);
        }

        public int DataCountPerPage
        {
            get => _dataCountPerPage;
            set => SetProperty(ref _dataCountPerPage, value);
        }

        public int TotalCount
        {
            get => _totalCount;
            set => SetProperty(ref _totalCount, value);
        }

        public int MaxPageCount
        {
            get => _maxPageCount;
            set => SetProperty(ref _maxPageCount, value);
        }

        public ObservableCollection<T> Data
        {
            get => _data;
            set => SetProperty(ref _data, value);
        }

        //public string PageInfo
        //{
        //    get => _pageInfo;
        //    set => SetProperty(ref _pageInfo, value);
        //}

        // 页面大小选项
        public Dictionary<int, string> PageSizes { get; } = new Dictionary<int, string>
        {
            { 10, "10条/页" },
            { 20, "20条/页" },
            { 30, "30条/页" },
            { 40, "40条/页" },
            { 50, "50条/页" },
            { 100, "100条/页" }
        };

        public DelegateCommand PageUpdatedCmd { get; private set; }

        public DelegateCommand PageSizeUpdatedCmd { get; private set; }

        private Func<Task<(IEnumerable<T> data, int totalCount)>> _fetchData;

        public PaginationDataGridViewModel(Func<Task<(IEnumerable<T> data, int totalCount)>> fetchData)
        {
            _fetchData = fetchData;
            PageUpdatedCmd = new DelegateCommand(async () => await LoadDataAsync());
            PageSizeUpdatedCmd = new DelegateCommand(async () => await LoadDataAsync());

            // 初始化时加载数据
            //_ = LoadData();
        }

        public async Task LoadDataAsync()
        {
            try
            {
                var (data, totalCount) = await _fetchData();
                Data = new ObservableCollection<T>(data);
                TotalCount = totalCount;
                // 通知分页总数变化
                //RaisePropertyChanged(nameof(MaxPageCount));
                MaxPageCount = totalCount == 0 ? 1 : (int)Math.Ceiling((double)totalCount / DataCountPerPage);
                // 更新分页信息
                //PageInfo = $"共 {_totalCount} 条";
            }  catch (OperationCanceledException)
            {
                // 查询被取消，保持表格不变
                return;
            }
        }
    }


}
